使用numpy提取每行的最小值 您所在的位置:网站首页 python 使用numpy提取每行的最小值 使用numpy提取每行的最小值

使用numpy提取每行的最小值

2023-06-29 20:50| 来源: 网络整理| 查看: 265

本文介绍了使用numpy提取每行的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个问题,无法在互联网上或在此网站上找到答案.我相信这很容易.假设我有一组20个数字,并且它们以5x4矩阵的形式存在:

I have a question and I could not find the answer on the internet nor on this website. I am sure it is very easy though. Let's say I have a set of 20 numbers and I have them in a 5x4 matrix:

numbers = np.arange(20).reshape(5,4)

这将产生以下矩阵:

[ 0, 1, 2, 3] [ 4, 5, 6, 7] [ 8, 9, 10, 11] [12, 13, 14, 15] [16, 17, 18, 19]

现在,我希望每行的最小值为0,4,8,12,16.但是,我想补充一点,对于我的问题,最小值并不总是在第一列中,它可以在矩阵中的任意位置(即每一行的第一,第二,第三或第四列).如果有人可以对此有所了解,将不胜感激.

Now I would like to have the minimum value of each row, in this case amounting to 0,4,8,12,16. However, I would like to add that for my problem the minimum value is NOT always in the first column, it can be at a random place in the matrix (i.e. first, second, third or fourth column for each row). If someone could shed some light on this it would be greatly appreciated.

推荐答案

您只需要指定要采用最小值的轴即可.要在每一行中找到最小值,您需要指定轴1:

You just need to specify the axis across which you want to take the minimum. To find the minimum value in each row, you need to specify axis 1:

>>> numbers.min(axis=1) array([ 0, 4, 8, 12, 16])

对于2D数组,numbers.min()在数组中找到单个最小值,numbers.min(axis=0)返回每一列的最小值,numbers.min(axis=1)返回每一行的最小值.

For a 2D array, numbers.min() finds the single minimum value in the array, numbers.min(axis=0) returns the minimum value for each column and numbers.min(axis=1) returns the minimum value for each row.

这篇关于使用numpy提取每行的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有